package com.dabai.kcbalarmclock;

import androidx.annotation.IdRes;
import androidx.appcompat.app.AppCompatActivity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.provider.AlarmClock;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import com.google.android.material.datepicker.MaterialDatePicker;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Random;

public class MainActivity extends AppCompatActivity {

    private String classname, starttime;
    private RadioGroup rg1;
    LinearLayout ll1;
    ChipGroup cg1;

    ArrayList<Integer> weekdays;
    private BottomSheetDialog bsd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        bsd = new BottomSheetDialog(this);
        bsd.setContentView(R.layout.activity_main);
        bsd.show();

        bsd.setOnDismissListener(new DialogInterface.OnDismissListener() {
            @Override
            public void onDismiss(DialogInterface dialogInterface) {
                finish();
            }
        });

        weekdays = new ArrayList<>();

        TextView te1 = bsd.findViewById(R.id.textView);
        TextView te2 = bsd.findViewById(R.id.textView2);
        ll1 = bsd.findViewById(R.id.ll1);
        cg1 = bsd.findViewById(R.id.cg1);


        try {
            Intent intent = getIntent();

            Uri uri = Uri.parse(intent.getDataString());
            classname = uri.getQueryParameter("classname");
            classname = classname.substring(classname.indexOf(">") + 1, classname.indexOf("</"));
            starttime = uri.getQueryParameter("start");

            te1.setText("课程名称:" + classname);
            te2.setText("提醒时间:" + starttime);
        } catch (Exception e) {
            bsd.dismiss();
            finish();
            Toast.makeText(this, "有点小问题!", Toast.LENGTH_SHORT).show();
        }


        rg1 = bsd.findViewById(R.id.rg);
        rg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                switch (i) {
                    case R.id.three:
                        //自定义
                        ll1.setVisibility(View.VISIBLE);
                        break;
                    default:
                        ll1.setVisibility(View.GONE);
                        break;
                }

            }
        });
    }


    public int setdays() {
        Chip chip0 = bsd.findViewById(R.id.chip0);
        if (chip0.isChecked()) {
            weekdays.add(Calendar.SUNDAY);
        }

        Chip chip1 = bsd.findViewById(R.id.chip1);
        if (chip1.isChecked()) {
            weekdays.add(Calendar.MONDAY);
        }
        Chip chip2 = bsd.findViewById(R.id.chip2);
        if (chip2.isChecked()) {
            weekdays.add(Calendar.TUESDAY);
        }

        Chip chip3 = bsd.findViewById(R.id.chip3);
        if (chip3.isChecked()) {
            weekdays.add(Calendar.WEDNESDAY);
        }
        Chip chip4 = bsd.findViewById(R.id.chip4);
        if (chip4.isChecked()) {
            weekdays.add(Calendar.THURSDAY);
        }

        Chip chip5 = bsd.findViewById(R.id.chip5);
        if (chip5.isChecked()) {
            weekdays.add(Calendar.FRIDAY);
        }

        Chip chip6 = bsd.findViewById(R.id.chip6);
        if (chip6.isChecked()) {
            weekdays.add(Calendar.SATURDAY);
        }

        return weekdays.size();
    }

    public void setAlock(View view) {

        switch (rg1.getCheckedRadioButtonId()) {
            case R.id.first:
                createAlarm("上课提醒:" + classname, Integer.parseInt(starttime.split(":")[0]), Integer.parseInt(starttime.split(":")[1]), new Random().nextInt(1000));
                break;
            case R.id.second:
                //每天
                ArrayList<Integer> tmpdays = new ArrayList<>();
                tmpdays.add(Calendar.MONDAY);
                tmpdays.add(Calendar.THURSDAY);
                tmpdays.add(Calendar.TUESDAY);
                tmpdays.add(Calendar.FRIDAY);
                tmpdays.add(Calendar.WEDNESDAY);
                tmpdays.add(Calendar.SATURDAY);
                tmpdays.add(Calendar.SUNDAY);
                createAlarm(tmpdays, "上课提醒:" + classname, Integer.parseInt(starttime.split(":")[0]), Integer.parseInt(starttime.split(":")[1]), new Random().nextInt(1000));
                break;

            case R.id.three:
                //自定义
                if (setdays() <= 0) {
                    Toast.makeText(this, "自定义日期必须选择一个", Toast.LENGTH_SHORT).show();
                    return;
                }
                createAlarm(weekdays, "上课提醒:" + classname, Integer.parseInt(starttime.split(":")[0]), Integer.parseInt(starttime.split(":")[1]), new Random().nextInt(1000));
                break;
        }
    }

    private void createAlarm(String message, int hour, int minutes, int resId) {
        String packageName = getApplication().getPackageName();
        Uri ringtoneUri = Uri.parse("android.resource://" + packageName + "/" + resId);
        //action为AlarmClock.ACTION_SET_ALARM
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                //闹钟的小时
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                //闹钟的分钟
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes)
                //响铃时提示的信息
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                //用于指定该闹铃触发时是否振动
                .putExtra(AlarmClock.EXTRA_VIBRATE, true)
                //一个 content: URI，用于指定闹铃使用的铃声，也可指定 VALUE_RINGTONE_SILENT 以不使用铃声。
                //如需使用默认铃声，则无需指定此 extra。
                .putExtra(AlarmClock.EXTRA_RINGTONE, ringtoneUri)
                //一个 ArrayList，其中包括应重复触发该闹铃的每个周日。
                // 每一天都必须使用 Calendar 类中的某个整型值（如 MONDAY）进行声明。
                //对于一次性闹铃，无需指定此 extra
                //.putExtra(AlarmClock.EXTRA_DAYS, testDays)
                //如果为true，则调用startActivity()不会进入手机的闹钟设置界面
                .putExtra(AlarmClock.EXTRA_SKIP_UI, false);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }

    private void createAlarm(ArrayList<Integer> weekDays, String message, int hour, int minutes, int resId) {
        String packageName = getApplication().getPackageName();
        Uri ringtoneUri = Uri.parse("android.resource://" + packageName + "/" + resId);
        //action为AlarmClock.ACTION_SET_ALARM
        Intent intent = new Intent(AlarmClock.ACTION_SET_ALARM)
                //闹钟的小时
                .putExtra(AlarmClock.EXTRA_HOUR, hour)
                //闹钟的分钟
                .putExtra(AlarmClock.EXTRA_MINUTES, minutes)
                //响铃时提示的信息
                .putExtra(AlarmClock.EXTRA_MESSAGE, message)
                //用于指定该闹铃触发时是否振动
                .putExtra(AlarmClock.EXTRA_VIBRATE, true)
                //一个 content: URI，用于指定闹铃使用的铃声，也可指定 VALUE_RINGTONE_SILENT 以不使用铃声。
                //如需使用默认铃声，则无需指定此 extra。
                .putExtra(AlarmClock.EXTRA_RINGTONE, ringtoneUri)
                //一个 ArrayList，其中包括应重复触发该闹铃的每个周日。
                // 每一天都必须使用 Calendar 类中的某个整型值（如 MONDAY）进行声明。
                //对于一次性闹铃，无需指定此 extra
                .putExtra(AlarmClock.EXTRA_DAYS, weekDays)
                //如果为true，则调用startActivity()不会进入手机的闹钟设置界面
                .putExtra(AlarmClock.EXTRA_SKIP_UI, false);
        if (intent.resolveActivity(getPackageManager()) != null) {
            startActivity(intent);
        }
    }



}

